﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MentalAlchemy.Molecules.MachineLearning
{
	public class EvoPCA3 : EvoPCA2
	{
		public EvoPCA3 ()
		{
			Crossing = EAElements.WeightedCrossBatch;
		}

		public override void Init(EAParameters parameters)
		{
			parameters.PopulationSize = parameters.ParentsNumber;
			base.Init(parameters);
		}

		public override void Continue(EAParameters parameters)
		{
			//
			// validate [parameters].
			if (FitnessFunction != null && !EAElements.ValidateParameters(parameters)) throw new Exception("[EvolutionaryAlgorithm.Run]: Invalid parameters setting or fitness function is undefined.");

			CurrentGeneration = 1;
			Evaluate();
			popul = EAElements.Sort(popul);	// sort individuals.

			for (; CurrentGeneration <= parameters.GenerationsNumber; ++CurrentGeneration, CollectStats())
			{
				// create children.
				parameters.PopulationSize = parameters.OffspringNumber;
				selPopul = Crossing(popul, parameters);
				Mutation(selPopul, parameters);

				// evaluate children.
				Individual childBest;
				Evaluation(selPopul, FitnessFunction, out childBest);

				// merge two populations and perform selection.
				popul.AddRange(selPopul);
				popul = EAElements.TruncationSelection(popul, parameters.PopulationSize);

				// save the best.
				bestInd = (Individual)popul[0].Clone();
			}

			//Evaluate();	// final population evaluation.
		}
	}
}
